package com.apache.match.queue;

import java.util.Arrays;

public class KeywordDesc extends Keyword {

    public KeywordDesc() {
        super();
    }

    public KeywordDesc(long priceUnit, long timestamp, String id) {
        super(priceUnit, timestamp, id);
    }

    // 实现Comparable接口的抽象方法,定义排序规则

    public int compareTo(Object o) {
        // 定义排序规则: Keyword对象里的属性i大的则排序时比较"大"(排序靠后)

        if (o instanceof KeywordDesc) {
            if (this.priceUnit < ((KeywordDesc) o).priceUnit) {
                return 1;
            } else if (this.priceUnit > ((KeywordDesc) o).priceUnit) {
                return -1;
            } else if (this.priceUnit == ((KeywordDesc) o).priceUnit) {
                if (this.timestamp > ((KeywordDesc) o).timestamp) {
                    return 1;
                } else if (this.timestamp < ((KeywordDesc) o).timestamp) {
                    return -1;
                } else {
                    if (this.id.compareTo(((KeywordDesc) o).id) > 0) {
                        return 1;
                    } else if (this.id.compareTo(((KeywordDesc) o).id) < 0) {
                        return -1;
                    }
                }
            }
            return 0;
        } else {
            // 非Keyword对象与之比较,则抛出异常

            throw new ClassCastException("Can't compare");
        }
    }

    // 测试

    public static void main(String[] args) {
        KeywordDesc[] c = new KeywordDesc[] { new KeywordDesc(10L, 1005L, "O1001"),
                new KeywordDesc(11, 1000, "O1002"), new KeywordDesc(10, 1000, "O1002"),
                new KeywordDesc(9, 1000, "O1003"), new KeywordDesc(10, 1005, "O1000") };
        // 排序前

        System.out.println(Arrays.asList(c));
        // 排序

        Arrays.sort(c);
        // 排序后

        System.out.println(Arrays.asList(c));
    }

}
